<template>
  <div class="competition-form">
    <el-form :model="form" label-width="120px" size="small">
      <el-form-item label="竞赛名称" required>
        <el-input v-model="form.competitionName" placeholder="请输入竞赛名称" clearable />
      </el-form-item>

      <el-form-item label="竞赛级别" required>
        <el-select v-model="form.competitionLevel" placeholder="请选择级别" clearable>
          <el-option label="国家级" value="国家级" />
          <el-option label="省级" value="省级" />
          <el-option label="市级" value="市级" />
          <el-option label="校级" value="校级" />
          <el-option label="CCF" value="CCF" />
        </el-select>
      </el-form-item>

      <el-form-item label="竞赛类别" required>
        <el-select v-model="form.competitionCategory" placeholder="请选择类别" clearable>
          <el-option label="A1" value="A1" />
          <el-option label="A2" value="A2" />
          <el-option label="A3" value="A3" />
        </el-select>
      </el-form-item>

      <el-form-item label="获奖等级" required>
        <el-select v-model="form.awardLevel" placeholder="请选择获奖等级" clearable>
          <el-option label="一等奖" value="一等奖" />
          <el-option label="二等奖" value="二等奖" />
          <el-option label="三等奖" value="三等奖" />
        </el-select>
      </el-form-item>

      <el-form-item label="是否团队赛" required>
        <el-radio-group v-model="form.isTeam">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="form.isTeam" label="团队中角色">
        <el-input v-model="form.roleInTeam" placeholder="请输入在团队中的角色" clearable />
      </el-form-item>

      <el-form-item v-if="previewScore > 0" label="预计得分">
        <el-tag type="success" size="large">{{ previewScore.toFixed(2) }} 分</el-tag>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleSave">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'CompetitionForm',
  data() {
    return {
      form: {
        competitionName: '',
        competitionLevel: '',
        competitionCategory: '',
        awardLevel: '',
        isTeam: false,
        roleInTeam: ''
      },
      previewScore: 0
    }
  },
  watch: {
    'form.competitionLevel': 'calculatePreviewScore',
    'form.competitionCategory': 'calculatePreviewScore',
    'form.awardLevel': 'calculatePreviewScore',
    'form.isTeam': 'calculatePreviewScore'
  },
  methods: {
    calculatePreviewScore() {
      const { competitionLevel, competitionCategory, awardLevel, isTeam } = this.form
      
      if (!competitionLevel || !competitionCategory || !awardLevel) {
        this.previewScore = 0
        return
      }
      
      let baseScore = 0
      
      
      switch (competitionLevel) {
        case '国家级':
          baseScore = 30
          break
        case '省级':
          baseScore = 15
          break
        case '市级':
          baseScore = 4
          break
        case '校级':
          baseScore = 1.5
          break
        case 'CCF':
          baseScore = 0.2
          break
        default:
          baseScore = 0
      }
      
     
      switch (competitionCategory) {
        case 'A1':
          baseScore *= 1.0
          break
        case 'A2':
          baseScore *= 0.67
          break
        case 'A3':
          baseScore *= 0.5
          break
      }
      
     
      switch (awardLevel) {
        case '一等奖':
          baseScore *= 1.0
          break
        case '二等奖':
          baseScore *= 0.83
          break
        case '三等奖':
          baseScore *= 0.67
          break
      }
      
      
      if (isTeam) {
        baseScore *= 0.8
      }
      
      this.previewScore = Math.round(baseScore * 100) / 100
    },
    
    async handleSave() {
      const {
        competitionName,
        competitionLevel,
        competitionCategory,
        awardLevel,
        isTeam,
        roleInTeam
      } = this.form

      if (!competitionName || !competitionLevel || !competitionCategory || !awardLevel) {
        ElMessage.error('请完整填写所有必填字段')
        return
      }

      const studentId = localStorage.getItem('studentId')
      if (!studentId) {
        ElMessage.error('未检测到学号，请重新登录')
        return
      }

      const payload = {
        studentId: Number(studentId),
        competitionName,
        level: competitionLevel,
        category: competitionCategory,
        prize: awardLevel,
        isTeam,
        roleInTeam: isTeam ? roleInTeam : null
      }

      try {
        const res = await axios.post('/api/competitions', payload, {
          headers: { 'Content-Type': 'application/json' },
          withCredentials: true
        })

        if (res.data > 0 || (res.data.code && res.data.code === 200)) {
          ElMessage.success('学科竞赛保存成功')
          this.resetForm()
        } else {
          ElMessage.error(res.data.message || '保存失败，请稍后重试')
        }
      } catch (error) {
        console.error('保存失败:', error)
        ElMessage.error('保存失败，请稍后重试')
      }
    },

    resetForm() {
      this.form = {
        competitionName: '',
        competitionLevel: '',
        competitionCategory: '',
        awardLevel: '',
        isTeam: false,
        roleInTeam: ''
      }
      this.previewScore = 0
    }
  }
}
</script>

<style scoped>
.competition-form {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background-color: #fafafa;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}
</style>
